<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 系统ajax
 * @package  ajax
 * @subpackage  backend
 * @author   allen.qiang
 */

class ajax_system {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询消息
                    $this->searchMessages();
                    break;

                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {

            $this->toReturn('参数错误');

        }

    }

    /**
     * 返回数据公共方法
     * @param string $message 提示信息
     * @param int $code 代码
     * @param mixed $data 其他数据
     */
    private function toReturn( $message, $code = 400, $data = null) {

        if (is_null($data)) {
            die( json_encode( array( 'code' => $code, 'message' => $message ) ) );
        } else {
            die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );
        }

    }

    //查询消息
    private function searchMessages() {

        global $G_PGLOB;

        //查询系统消息
        $search_sql = 'SELECT * FROM sys_messages';
        $message_array = $G_PGLOB->DBCA->stmt_query($search_sql);
        if ($message_array) {
            foreach($message_array as &$message) {
                $item_array[] = array(
                    'id' => $message->id,
                    'message' => $message->message,
                    'link' => $message->link
                );
            }
            $this->toReturn('ok', 200, $item_array);
        } else {
            $this->toReturn('没有消息', 400);
        }

    }

}